package com.BigData.app.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.BigData.app.service.StatisticsService;

@RestController
public class StatisticsController {
	@Autowired
	StatisticsService statisticsService;
	@RequestMapping("/statistics/getInformation")
	List<int[]> getNovelPoint(String dataName, String website,String type){
		return statisticsService.getNovelPointService(dataName, website,type);
	}
	
	@RequestMapping("statistics/getWebsite")
	public List<String> getWebsite() {
		// TODO Auto-generated method stub
		return statisticsService.getWebsite();
	}
	
	@RequestMapping("statistics/getType")
	public List<String> getType(String website) {
		// TODO Auto-generated method stub
		return statisticsService.getType(website);
	}
	
	@RequestMapping("statistics/getReDu")
	public List<HashMap<String, Object> >getReDu(String website) {
		// TODO Auto-generated method stub
		List<HashMap<String, Object>> maplist = new ArrayList<HashMap<String, Object>>();//创建Map对象 Object是所有类型的父类
		List<String> typelist=statisticsService.getType(website);
		for(String type:typelist)
			
		{
			HashMap<String, Object> map = new HashMap<String, Object>(); 
			HashMap<String, Object> sum=statisticsService.getReDu(website, type);
			double zishu= Double.parseDouble(sum.get("zishu").toString());
			double tuijian= Double.parseDouble(sum.get("tuijian").toString());
			double yuepiao= Double.parseDouble(sum.get("yuepiao").toString());
			double dianji= Double.parseDouble(sum.get("dianji").toString());
			double dashang= Double.parseDouble(sum.get("dashang").toString());
			double xianhua= Double.parseDouble(sum.get("xianhua").toString());
			double jReview=Double.parseDouble(sum.get("jReview").toString());
			double jFavorite=Double.parseDouble(sum.get("jFavorite").toString());
			double jGifts= Double.parseDouble(sum.get("jGifts").toString());
			double jPoint= Double.parseDouble(sum.get("jPoint").toString());
			double redu=(tuijian*2+yuepiao*50+dianji+dashang*70+xianhua*1.5+jReview*10+jFavorite*50+jGifts*20+jPoint)/zishu*1000;
			map.put("type", type);//存储key和value
			map.put("redu", redu);//存储key和value
			maplist.add(map); 
		}			
		return maplist;
	}
	
	@RequestMapping("statistics/getReDuByType")
	public List<HashMap<String, Object>> getReDuByType(String type) {
		// TODO Auto-generated method stub
		List<HashMap<String, Object>> maplist = new ArrayList<HashMap<String, Object>>();//创建Map对象 Object是所有类型的父类
		List<String> websitelist=statisticsService.getWebsite();	
		for(String website:websitelist)
		{
			System.out.println(website);
			HashMap<String, Object> map = new HashMap<String, Object>(); 
			HashMap<String, Object> sum=statisticsService.getReDu(website, type);
			double zishu= Double.parseDouble(sum.get("zishu").toString());
			double tuijian= Double.parseDouble(sum.get("tuijian").toString());
			double yuepiao= Double.parseDouble(sum.get("yuepiao").toString());
			double dianji= Double.parseDouble(sum.get("dianji").toString());
			double dashang= Double.parseDouble(sum.get("dashang").toString());
			double xianhua= Double.parseDouble(sum.get("xianhua").toString());
			double jReview=Double.parseDouble(sum.get("jReview").toString());
			double jFavorite=Double.parseDouble(sum.get("jFavorite").toString());
			double jGifts= Double.parseDouble(sum.get("jGifts").toString());
			double jPoint= Double.parseDouble(sum.get("jPoint").toString());
			double redu;
			if(zishu!=0){
			redu=(tuijian*2+yuepiao*50+dianji+dashang*70+xianhua*1.5+jReview*10+jFavorite*50+jGifts*20+jPoint)/zishu*1000;
			}else{
				redu=0;
			}
			if(website.equals("起点"))
			{
					System.out.println("起点");
					redu*=1000;
			}
			map.put("name", website);//存储key和value
			map.put("value", redu);//存储key和value
			maplist.add(map); 
		}			
		return maplist;
	}
	
	@RequestMapping("statistics/getRecommendWebsite")
	public List<HashMap<String, Object>> getRecommendWebsite(String type) {
		// TODO Auto-generated method stub
		List<HashMap<String, Object>> maplist = new ArrayList<HashMap<String, Object>>();//创建Map对象 Object是所有类型的父类
		List<String> websitelist=statisticsService.getWebsite();
		double all=0;
		for(String website:websitelist)
		{
			System.out.println(website);
			HashMap<String, Object> map = new HashMap<String, Object>(); 
			HashMap<String, Object> sum=statisticsService.getReDu(website, type);
			double zishu= Double.parseDouble(sum.get("zishu").toString());
			double tuijian= Double.parseDouble(sum.get("tuijian").toString());
			double yuepiao= Double.parseDouble(sum.get("yuepiao").toString());
			double dianji= Double.parseDouble(sum.get("dianji").toString());
			double dashang= Double.parseDouble(sum.get("dashang").toString());
			double xianhua= Double.parseDouble(sum.get("xianhua").toString());
			double jReview=Double.parseDouble(sum.get("jReview").toString());
			double jFavorite=Double.parseDouble(sum.get("jFavorite").toString());
			double jGifts= Double.parseDouble(sum.get("jGifts").toString());
			double jPoint= Double.parseDouble(sum.get("jPoint").toString());
			double redu;
			if(zishu!=0){
			redu=(tuijian*2+yuepiao*50+dianji+dashang*70+xianhua*1.5+jReview*10+jFavorite*50+jGifts*20+jPoint)/zishu*1000;
			}else{
				redu=0;
			}
			if(website.equals("起点"))
			{
					System.out.println("起点");
					redu*=1000;
			}			
			map.put("name", website);//存储key和value
			map.put("value", redu);//存储key和value
			maplist.add(map); 
			all=all+redu;
			System.out.println(all);
		}	
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();//创建Map对象 Object是所有类型的父类
		for(HashMap<String, Object> temp:maplist)
		{
			HashMap<String, Object> map = new HashMap<String, Object>(); 
			String n=(String) temp.get("name");
			double v=(double) temp.get("value");
			map.put("name", n);//存储key和value
			map.put("value", v*100/all);//存储key和value
			list.add(map); 
		}
		return list;
	}
	
	@RequestMapping("statistics/getNovelByType")
	public List<HashMap<String, Object>> getNovelByType(String type) {
		// TODO Auto-generated method stub
		List<HashMap<String, Object>> maplist = new ArrayList<HashMap<String, Object>>();//创建Map对象 Object是所有类型的父类
		Map<String, Double> ret = new HashMap<String, Double>();//创建Map对象 Object是所有类型的父类
		List<HashMap<String, Object>> database=statisticsService.getNovelByType(type);	
		int f=0;
		for(HashMap<String, Object> novel:database)
		{			
			String tagList=novel.get("tag").toString();
			String[]tagArr=tagList.split("\\|");
			double zishu= Double.parseDouble(novel.get("zishu").toString());
			double tuijian= Double.parseDouble(novel.get("tuijian").toString());
			double yuepiao= Double.parseDouble(novel.get("yuepiao").toString());
			double dianji= Double.parseDouble(novel.get("dianji").toString());
			double dashang= Double.parseDouble(novel.get("dashang").toString());
			double xianhua= Double.parseDouble(novel.get("xianhua").toString());
			double jReview=Double.parseDouble(novel.get("jReview").toString());
			double jFavorite=Double.parseDouble(novel.get("jFavorite").toString());
			double jGifts= Double.parseDouble(novel.get("jGifts").toString());
			double jPoint= Double.parseDouble(novel.get("jPoint").toString());
			double redu=0;
			if(zishu!=0){
			redu=(tuijian*2+yuepiao*50+dianji+dashang*70+xianhua*1.5+jReview*10+jFavorite*50+jGifts*20+jPoint)/zishu*1000;
			}else{
				redu=0;
			}
			if(novel.get("website").toString().equals("起点"))
			{
					redu*=1000;
			}
			
			System.out.println("热度  "+redu);
			System.out.println("长度  "+tagArr.length);
			double singleRedu=(double) (redu/tagArr.length);
			/*System.out.println(singleRedu);*/
			
			for(String t:tagArr){
				if(f!=0){
					if((ret.get(t))!=null){
						ret.put(t, ret.get(t)+singleRedu);
					}else{
						ret.put(t, singleRedu);
					}
				}else{
					ret.put(t, singleRedu);
					f++;
				}				
			}			
		}
		//排序
		List<Map.Entry<String, Double>> wordMap = new ArrayList<Map.Entry<String, Double>>(ret.entrySet());
		Collections.sort(wordMap, new Comparator<Map.Entry<String, Double>>() {// 根据value排序
			public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
			double result = o2.getValue() - o1.getValue();
			if (result > 0)
			return 1;
			else if (result == 0)
			return 0;
			else
			return -1;
			}
			});
		int i=0;
		double count=0;
		for(Map.Entry<String, Double> set:wordMap){
			if(i==0||i+3>=wordMap.size()){
				count+=set.getValue();				
			}
			i++;
			}
		System.out.println(count);
		for(Map.Entry<String, Double> set:wordMap){
			HashMap<String, Object> map = new HashMap<String, Object>(); 
			map.put("tag", set.getKey());//存储key和value
			map.put("value", set.getValue()*100/count);//存储key和value
			maplist.add(map); 
			/*System.out.println(set.getKey() +"   "+set.getValue());*/
			}
		return maplist;
	}
}
